package restless.aimovement.astar {
		
	import flash.geom.Point;
	import restless.aimovement.astar.analyzers.AvoiderAnalyzer;
	import restless.aimovement.astar.analyzers.FullClippingAnalyzer;
	import restless.aimovement.astar.analyzers.WalkableAnalyzer;
	import restless.aimovement.astar.interfaces.ICallAStar;
	import restless.aimovement.astar.interfaces.IMap;
	import restless.utilities.numeric.RLERandom;
	
	public class AstarMap {
		
		public var map:IMap;
		protected var astar:Astar;
		
		public function AstarMap(map:IMap) {
			this.map = map;
		}
		
		public function aStarCalc(startCol:int, startRow:int, targetCol:int, targetRow:int, obj:ICallAStar):void {
			astar = new Astar();
			astar.addAnalyzer(new WalkableAnalyzer());
			astar.addAnalyzer(new FullClippingAnalyzer());
			astar.addEventListener(AstarEvent.PATH_FOUND, obj.getPath);
			astar.addEventListener(AstarEvent.PATH_NOT_FOUND, onPathNotFound);
			astar.getPath( new PathRequest( new Point(startCol, startRow), new Point(targetCol, targetRow), map) );
		}
		
		public function aStarFlee(startCol:int, startRow:int, fleeFromCol:int, fleeFromRow:int, obj:ICallAStar):void {
			var pPlayer:Point = new Point (fleeFromCol, fleeFromRow);
			var tempPoint:Point = new Point();
			var num:Number;
			var bool:Boolean;
			var tempTile:*;
			
			do {
				tempPoint.x = RLERandom.randomIntMinMax(20, 0, true);
				tempPoint.y = RLERandom.randomIntMinMax(20, 0, true);
				tempTile = map.getTileAt(tempPoint);
				num = tempTile.cost;
				bool = tempTile.walkable;
				
			} while ( num  > 1 && bool == false)
			
			astar = new Astar();
			astar.addAnalyzer(new WalkableAnalyzer());
			astar.addAnalyzer(new FullClippingAnalyzer());
			astar.addAnalyzer(new AvoiderAnalyzer(pPlayer, 5));
			astar.addEventListener(AstarEvent.PATH_FOUND, obj.getPath); //onPathFound
			astar.addEventListener(AstarEvent.PATH_NOT_FOUND, onPathNotFound);
			astar.getPath( new PathRequest( new Point(startCol, startRow), tempPoint, map) );
		}
		
		public function onPathNotFound(e:AstarEvent):void {
			trace(e.currentTarget + " could not find a path!");
		}
	}
}